package AlgoMethod

import "fmt"

const QueenNum int = 8
var PlaceResult [QueenNum]int

func InitChess() {
	for i := 0; i < QueenNum; i++ {
		PlaceResult[i] = -1
	}
}

func PrintChees() {
	for i := 0; i < QueenNum; i++ {
		for j := 0; j < QueenNum; j++ {
			if PlaceResult[i] == j {
				fmt.Printf("%d ", 1)
			} else {
				fmt.Printf("%d ", 0)
			}
		}
		fmt.Println()
	}
	fmt.Println()
}

func IsOk(row, col int) bool {
	leftup := col - 1
	rightup := col + 1
	for i := row - 1; i >= 0; i-- {
		if PlaceResult[i] == col {
			return false
		}
		if leftup >= 0 {
			if PlaceResult[i] == leftup {
				return false
			}
		}
		if rightup < QueenNum {
			if PlaceResult[i] == rightup {
				return false
			}
		}
		leftup--
		rightup++
	}
	return true
}

func PlaceChess(row int)  {
	if row >= QueenNum {
		PrintChees()
		return
	}
	for col := 0; col < QueenNum; col++ {
		if IsOk(row, col) {
			PlaceResult[row] = col
			PlaceChess(row + 1)
		}
	}
}

func GetAllCombin(data []int, max uint) {
	x := 1<<max
	for i := 0; i < x; i++ {
		fmt.Print("[")
		var j uint
		for ; j < max; j++  {
			if (i & (1<<j)) > 0 {
				fmt.Printf("%d,", data[j])
			}
		}
		fmt.Println("]")
	}
}

const MAX_DATA_LEN = 3
var data = [MAX_DATA_LEN]int{1,2,3}
func GetAllCombinR(result [3]int, resultlen int, len int) {
	if len >= MAX_DATA_LEN {
		fmt.Print("[")
		for i:=0; i < resultlen; i++ {
			fmt.Printf("%d,", result[i])
		}
		fmt.Println("]")
		return
	}
	GetAllCombinR(result, resultlen, len+1)
	result[resultlen]=data[len]
	resultlen++
	GetAllCombinR(result, resultlen, len+1)
}